<?php
namespace bc\model\distribution;
use bc\ar\MActiveRecord;

class DistributionModel extends MActiveRecord{
    private $_self_error;//错误信息

    public static function tableName()
    {
        return "{{%product_distribution}}";
    }

    public $autoId="distribution_id";

    public function rules()
    {
        return array(
            array(array('distribution_price','product_id','first_discount','level_1','level_2','level_3'),'required'),
            array(array('create_time','is_del'),'required','on'=>array('add')),
            array(array('update_time','distribution_id'),'required','on'=>array('edit'))
        );
    }


    public function scenarios()
    {
        $old_scenarios=parent::scenarios();
        $new_scenarios= array(
            'edit'=>array('distribution_price','product_id','first_discount','level_1','level_2','level_3','update_time'),
            'add'=>array('distribution_id','distribution_price','product_id','first_discount','level_1','level_2','level_3','is_del','create_time','update_time'),
        );
        return array_merge($old_scenarios,$new_scenarios);
    }

    public function attributeLabels()
    {
        return array(
            'distribution_id'=>'数据id',
            'distribution_price'=>'商品售价',
            'product_id'=>'商品选择',
            'first_discount'=>'首次折扣',
            'level_1'=>'一级分成比例',
            'level_2'=>'二级分成比例',
            'level_3'=>'三级分成比例',
            'update_time'=>'更新时间',
            'is_del'=>'是否删除',
            'create_time'=>'创建时间',
        );
    }

    /**
     * Created by Mr.hu.
     * @param $data 表单提交的数据
     * @param $type 1:添加  2：编辑
     * Description:验证表单提交的数据，添加或编辑
     */
    public function create_data($data,$type)
    {
        //添加的验证
        if($type==1){
            return $this->add_validate($data);
        }

        //编辑的验证
        elseif($type==2){
            return $this->edit_validate($data);
        }

    }

    /**
     * Created by Mr.hu.
     * @param $data
     * Description:添加的验证
     */
    private function add_validate($data)
    {
        $new_data=array();
        if(empty($data['distribution_price'])){
            $this->_self_error='商品售价不能为空';
            return false;
        }
        if(!is_numeric($data['distribution_price'])||intval($data['distribution_price']<=0)){
            $this->_self_error='商品售价是不为0的数';
            return false;
        }
        $new_data['distribution_price']=$data['distribution_price'];

        if(empty($data['product_id'])){
            $this->_self_error='请选择商品';
            return false;
        }

    }

    /**
     * Created by Mr.hu.
     * @param $data
     * Description:修改的验证
     */
    private function edit_validate($data)
    {

    }


    /**
     * Created by Mr.hu.
     * @param $id
     * Description:获取某一个分销商品的信息
     */
    public function getOneInfo($id)
    {
        $master_info=self::findOne(['distribution_id'=>$id])->toArray();
        $slave_info=$this->find()->where(['distribution_id'=>$id])->asArray()->all();
        $master_info['images']=$slave_info;
        return $master_info;
    }


}